import { DEL_NEWS,READ_NEWS,ALL_READ } from "./actionTypes";

let defaultState = {
	msgLists:[
		{
			id:1,
			title:"消息1",
			content:"多好玩ID我hi单多好玩ID我hi单京东网喜欢此外多好玩ID我hi单位hi京东网喜欢此外位hi京东网喜欢此外",
			read: false
		},
		{
			id:2,
			title:"消息2",
			content:"多好玩ID我hi单位hi京东网多好玩喜欢此外222多好玩ID我hi单位hi京东网喜欢此外222多好玩ID我hi单位hi京东网喜欢此外222多好玩ID我hi单位hi京东网喜欢此外222喜欢此外222",
			read: true
		},
		{
			id:3,
			title:"消息3",
			content:"多好玩ID我为和好烦isa定居hi单位hi33434京东网喜欢此外",
			read: true
		},
		{
			id:4,
			title:"消息4",
			content:"多好玩ID我hi单位hi京多好玩ID我hi单位hi京单位hi京fefe东网喜欢此外多好玩ID我hi单位hi京fefe东网喜欢此外fefe东网喜欢此外",
			read: false
		},
		{
			id:5,
			title:"消息5",
			content:"多好玩ID我hi单位hi京多好玩ID我hi单单位hi京fefe东网欢此外多好玩ID我hi单位hi京fefe东网喜欢此外多好玩ID我hi单位hi京fefe东网喜欢此外fefe东网喜欢此外",
			read: true
		}
	]
}
// 去缓存  判断 缓存有没有值 有赋值 没有不赋值
let reduxState = localStorage.getItem('reduxState');
if(reduxState){
  defaultState = JSON.parse(reduxState)
}

const msgReducer = (state=defaultState,action)=>{
	let newState = JSON.parse(JSON.stringify(state));
	switch(action.type){
    case DEL_NEWS:
			 newState.msgLists.splice(newState.msgLists.findIndex((el)=>el.id===action.value),1)
			 console.log(newState.msgLists)
			console.log(action.value)
      break;
    case READ_NEWS:
			newState.msgLists[newState.msgLists.findIndex((el)=>el.id===action.value)].read=true;
			break;
		case ALL_READ:
			newState.msgLists.forEach((el)=>el.read=true);
    default : break;
  }
  localStorage.setItem('reduxState',JSON.stringify(newState))
	return newState;
}

export default msgReducer